package Dao;

import DataClass.Course;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseInfoDao {

    private static PreparedStatement getCourseInfoStatement = null;

    public static List<Course> getCourseInfo() throws SQLException {
        List<Course> list = new ArrayList<>();
        ResultSet data;
        try{
            data = CourseInfoDao.getCourseInfoData();
        }catch (SQLException e){
            System.out.println("授课信息查询失败 from Dao");
            throw new SQLException();
        }

        while(data.next()) {
            String Cid = data.getString("TIno");
            String Cname = data.getString("TIname");
            String Ctype = data.getString("TItype");
            String Cstudentlevel = data.getString("TIstudentLevel");
            String Ctime = data.getString("TIsourceTime");
            String Cteacher = data.getString("TImajorTeacher");
            list.add(new Course(Cid, Cname, Ctype, Cstudentlevel, Ctime, Cteacher));
        }
        JBDCUtil.closeResultSet(data);
        return list;
    }

    /**
     * 获取课程信息
     * @return 返回课程信息resultSet
     * @throws SQLException 数据库异常
     */
    public static ResultSet getCourseInfoData() throws SQLException {
        if(getCourseInfoStatement == null || getCourseInfoStatement.isClosed()) {
            String sql = "SELECT * FROM Teaching_Information";
            getCourseInfoStatement = JBDCUtil.getPreparedStatement(sql);
        }
        ResultSet result = getCourseInfoStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }
}
